package rsi

import (
	"go2o/core/domain/interface/ad"

	"github.com/ixre/gof/storage"
)

type adService struct {
	_rep    ad.IAdRepo
	storage storage.Interface
}

func NewAdvertisementService(rep ad.IAdRepo, storage storage.Interface) *adService {
	return &adService{
		_rep:    rep,
		storage: storage,
	}
}

func (a *adService) getUserAd(adUserID int32) ad.IUserAd {
	return a._rep.GetAdManager().GetUserAd(adUserID)
}

func (a *adService) GetAdGroupById(id int32) ad.AdGroup {
	return a._rep.GetAdManager().GetAdGroup(id).GetValue()
}

func (a *adService) DelAdGroup(id int32) error {
	return a._rep.GetAdManager().DelAdGroup(id)
}

func (a *adService) SaveAdGroup(d *ad.AdGroup) (int32, error) {
	m := a._rep.GetAdManager()
	var e ad.IAdGroup
	if d.ID > 0 {
		e = m.GetAdGroup(d.ID)
	} else {
		e = m.CreateAdGroup(d.Name)
	}
	err := e.SetValue(d)
	if err == nil {
		return e.Save()
	}
	return 0, err
}

func (a *adService) GetGroups() []ad.AdGroup {
	m := a._rep.GetAdManager()
	list := m.GetAdGroups()
	list2 := []ad.AdGroup{}
	for _, v := range list {
		list2 = append(list2, v.GetValue())
	}
	return list2
}
